// 引入mock
const Mock = require('mockjs')

// 使用mock生成随机数据
const myFoodData = Mock.mock({
    'data|3' : [
     { "foodId|+1" : 1,
        "foodName":"@ctitle(3, 5)",
        "foodPrice":'@float(10,100,0,2)',
        "foodImg":"@image('300x500', '#4A7BF7', 'Hello')",
        "foodExplain":"	@cparagraph(2)",
        "remarks":"@csentence(3, 5)",
        "businessId":"@inteager(1,10)"
    }
    ]
  })

module.exports = [

  //获取角色对应的权限列表
 {
  url: '/test/role/permissions',
  type: 'get',
  response: config => {
    const params = config.query;
    console.log("````````获取角色对应的权限列表接口，请求参数：%o",params);
    let roleId=params.roleId;
    //模拟数据
    let systemPermissions = [
      {
        id: 1, label: "A", api: "", children: [
          { id: 2, label: "A1", api: "GET /a1" },
          { id: 3, label: "A2", api: "GET /a2" },
          { id: 4, label: "A3", api: "POST /a3" },
          { id: 5, label: "A4", api: "PUT /a4" },
        ]
      },
      {
        id: 6, label: "B", api: "", children: [
          { id: 7, label: "B1", api: "GET /b1" },
          { id: 8, label: "B2", api: "GET /b2" },
          { id: 9, label: "B3", api: "POST /b3" },
          { id: 10, label: "B4", api: "PUT /b4" },
        ]
      }
    ]
    let roleIdPermIdsMap=[
      {roleId:1,params:[1,6]},
      {roleId:2,params:[2,3,6]},
      {roleId:3,params:[1]},
      {roleId:4,params:[4,7]},

    ]
    let perms=roleIdPermIdsMap.filter(x =>x.roleId == roleId);

    return {
      code: 20000,
      data: {
        "systemPermissions": systemPermissions,
        "assignedPermissions": [1]
      }
    }
  }
},

    //更新角色对应的权限列表
 {
  url: '/test/role/permissions/edit',
  type: 'post',
  response: config => {
    const body = config.body;
    console.log("````````更新角色对应的权限列表接口，请求参数：%o",body);
    let roleId=params.roleId;
    //模拟数据
    let systemPermissions = [
      /* {
        id: 1, label: "A", api: "", children: [
          { id: 2, label: "A1", api: "GET /a1" },
          { id: 3, label: "A2", api: "GET /a2" },
          { id: 4, label: "A3", api: "POST /a3" },
          { id: 5, label: "A4", api: "PUT /a4" },
        ]
      },
      {
        id: 6, label: "B", api: "", children: [
          { id: 7, label: "B1", api: "GET /b1" },
          { id: 8, label: "B2", api: "GET /b2" },
          { id: 9, label: "B3", api: "POST /b3" },
          { id: 10, label: "B4", api: "PUT /b4" },
        ]
      } */
    ]
    /* let roleIdPermIdsMap=[
      {roleId:1,params:[1,6]},
      {roleId:2,params:[2,3,6]},
      {roleId:3,params:[1]},
      {roleId:4,params:[4,7]},

    ] */
    //let perms=roleIdPermIdsMap.filter(x =>x.roleId == roleId);

    return {
      code: 20000,
      data: {
        "systemPermissions": systemPermissions,
        "assignedPermissions": [1]
      }
    }
  }
},



// ---------食品列表接口----------
  {
    url: '/vue-admin-template/goods/list',
    type: 'get',
    response: config => {

      const params = config.query;
      console.log("````````食品列表接口参数````````````",params);
      // 处理查询
      const fId = (params.foodId.trim() == "") ? null : params.foodId.trim();
      const fName = (params.foodName.trim() == "") ? null : params.foodName.trim();
      const pageNo = +params.page;//页码
      const pageSize = +params.limit;//每页多少条
      let retData = [];
      // let hasData = false; //输入了查询条件但是没有查
      if(fId != null || fName != null){
        for (let x of myFoodData.data){
          // console.log("-------fId:%d,fName:%s,x:%o",fId,fName,x);
          //只输入食品编号
          if((fId != null && fId == x.foodId) && (fName == null)){
            retData.push(x);
          }
          //只输入食品名称
          if ((fId == null) &&
          (fName != null && x.foodName.indexOf(fName.trim()) != -1)) {
            retData.push(x);
          }
          //输入了食品名称和食品编号
          if ((fId != null && fId == x.foodId ) &&
          (fName != null  && x.foodName.indexOf(fName.trim()) != -1)) {
            retData.push(x);
          }
        }
      }else{
        retData = myFoodData.data;
      }




      //处理分页（将查询出来的结果，按照粉也要求展示适当的数据）
      let startIndex = (pageNo-1)*pageSize;
      let endIndex = startIndex + (pageSize);
      retData=retData.slice(startIndex,endIndex)
      console.log(startIndex,endIndex);
      // const fn = config.query.foodName;
      // console.log(`食品列表${fn}`);
      return {
        code: 20000,
        data: {
          "data":retData,
          "total":12
        },
      }
    }
  },


// ---------根据食品id获取食品信息----------
{
  url: '/vue-admin-template/goods/detail',
  type: 'get',
  response: config => {

   const params = config.query;
   console.log("-----------食品参数",params);
   let retData = {}
   myFoodData.data.forEach(x => {
    if (x.foodId == params.id){
      retData = x
    }
   });

    return {
      code: 20000,
      data: {
        "goods":retData,
      },
    }
  }
},


// ---------删除食品信息----------
{
  url: '/vue-admin-template/goods/delete',
  type: 'post',
  response: config => {
  const params = config.body;
  console.log("-----------------params-----------",params);
  let foodIndex = -1
  myFoodData.data.forEach((x,index) =>{
    if (x.foodId == params.foodId) {
      foodIndex = index
    }
  })
  myFoodData.data.splice(foodIndex,1)
    return {
      code: 20000,
      data:"删除成功"
    }
  }
},

// ---------编辑食品信息----------

{
    url: '/vue-admin-template/goods/update',
    type: 'post',
    response: config => {
    let params = config.body;
    console.log("-----------------更新食品请求参数：%o-----------",params);
    let retData = myFoodData.data
    let foodIndex = -1
    myFoodData.data.forEach((x,index) =>{
      if (x.foodId == params.goods.foodId) {
        foodIndex = index
        // 方法二
        // x.foodName = params.goods.foodName
        // x.foodPrice = params.goods.foodPrice
        // x.foodImg = params.goods.foodImg
        // x.foodExplain = params.goods.foodExplain
        // x.remarks = params.goods.remarks
        // x.businessId = params.goods.businessId

        // 方法三
        // Object.assign(item,params.goods)

      }
    })
    retData.splice(foodIndex,1,params.goods)
      return {
        code: 20000,
        data:"更新成功"
      }
    }
  },

  // ---------添加食品信息----------
  {
    url: '/vue-admin-template/goods/create',
    type: 'post',
    response: config => {
    let params = config.body;
    console.log("-----------------添加食品请求参数：%o-----------",params);
    let retData = myFoodData.data

        retData.push(params.goods)
      return {
        code: 20000,
        data:"添加成功"
      }
    }
  },
]
